﻿Public Class fEncaListaPrecios
    Dim daEncaListaPrecios As lbGlobalcom.dsProductosTableAdapters.PRO_ENCALISTAPRECIOSTableAdapter
    Dim daDetListaPrecios As lbGlobalcom.dsProductosTableAdapters.PRO_DETALLELISTAPRECIOSTableAdapter
    Dim daDatosTablas As lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
    Dim daProductos As lbGlobalcom.dsProductosTableAdapters.PRO_PRODUCTOTableAdapter
    Private Sub PRO_ENCALISTAPRECIOSBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PRO_ENCALISTAPRECIOSBindingNavigatorSaveItem.Click
        Try
            PRO_ENCALISTAPRECIOSBindingSource.EndEdit()
            daEncaListaPrecios.Update(DsProductos.PRO_ENCALISTAPRECIOS)
            daDetListaPrecios.Update(DsProductos.PRO_DETALLELISTAPRECIOS)
            MessageBox.Show("Se salvarón registros con exito", "Salva Registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub fEncaListaPrecios_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        daEncaListaPrecios = New lbGlobalcom.dsProductosTableAdapters.PRO_ENCALISTAPRECIOSTableAdapter
        daDetListaPrecios = New lbGlobalcom.dsProductosTableAdapters.PRO_DETALLELISTAPRECIOSTableAdapter
        daDatosTablas = New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
        daProductos = New lbGlobalcom.dsProductosTableAdapters.PRO_PRODUCTOTableAdapter
        lbGlobalcom.cGeneral.ActivaControles(Me.Controls, False)
        DsProductos.PRO_ENCALISTAPRECIOS.Columns("FECHAINI").DefaultValue = Date.Today
        DsProductos.PRO_ENCALISTAPRECIOS.Columns("FECHAFIN").DefaultValue = Date.Today
        Try
            Dim dtEstado As lbGlobalcom.dsGlobal.GLO_DATOSTABLASDataTable = daDatosTablas.GetDataByIdgrupo("es")
            Dim dtListaDe As lbGlobalcom.dsGlobal.GLO_DATOSTABLASDataTable = daDatosTablas.GetDataByIdgrupo("ld")
            Dim dtTipolista As lbGlobalcom.dsGlobal.GLO_DATOSTABLASDataTable = daDatosTablas.GetDataByIdgrupo("tl")
            EstadoBindingSource.DataSource = dtEstado
            ListaDeBindingSource.DataSource = dtListaDe
            TipoListaBindingSource.DataSource = dtTipolista
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub


    Private Sub PRO_DETALLELISTAPRECIOSDataGridView_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles PRO_DETALLELISTAPRECIOSDataGridView.CellValidated
        Try
            If PRO_DETALLELISTAPRECIOSDataGridView.IsCurrentCellInEditMode Then
                Select Case e.ColumnIndex
                    Case 1
                        If PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value IsNot DBNull.Value Then _
                            Me.txtDescripProducto.Text = daProductos.GetNombreProducto(PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
                    Case 2, 3
                        If PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(2).Value IsNot DBNull.Value And PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(3).Value IsNot DBNull.Value Then
                            PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(4).Value = (1 - PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(2).Value) * PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(3).Value
                        End If

                End Select
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub PRO_DETALLELISTAPRECIOSDataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles PRO_DETALLELISTAPRECIOSDataGridView.CellValidating
        Try
            If PRO_DETALLELISTAPRECIOSDataGridView.IsCurrentCellInEditMode AndAlso e.FormattedValue IsNot Nothing AndAlso e.FormattedValue <> "" Then
                Select Case e.ColumnIndex
                    Case 1
                        If e.FormattedValue IsNot Nothing AndAlso e.FormattedValue IsNot DBNull.Value AndAlso e.FormattedValue <> "" Then
                            Dim NomProd As Object = daProductos.GetNombreProducto(e.FormattedValue)
                            If NomProd Is Nothing Then
                                MessageBox.Show("Producto con este codigo no existe, verifique e intente de nuevo", "No existe", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                                e.Cancel = True
                            End If
                        End If
                    Case 2
                        If Not IsNumeric(e.FormattedValue) Then
                            e.Cancel = True
                        End If
                    Case 3
                        If Not IsNumeric(e.FormattedValue) Then
                            e.Cancel = True
                        End If
                End Select
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    Private Sub PRO_DETALLELISTAPRECIOSDataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles PRO_DETALLELISTAPRECIOSDataGridView.DataError
        MessageBox.Show(e.Exception.Message)
    End Sub


    Private Sub PRO_DETALLELISTAPRECIOSDataGridView_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles PRO_DETALLELISTAPRECIOSDataGridView.GotFocus
        Try
            PRO_ENCALISTAPRECIOSBindingSource.EndEdit()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Try
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaLst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaLst.Click
        Dim fLPrecios As New fListaPrecios
        If fLPrecios.ShowDialog = Windows.Forms.DialogResult.OK Then
            DsProductos.PRO_DETALLELISTAPRECIOS.Rows.Clear()
            DsProductos.PRO_ENCALISTAPRECIOS.Rows.Clear()
            daEncaListaPrecios.FillByIdLista(DsProductos.PRO_ENCALISTAPRECIOS, fLPrecios.Lista)
            daDetListaPrecios.FillByIdLista(DsProductos.PRO_DETALLELISTAPRECIOS, fLPrecios.Lista)
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
        End If
    End Sub

    Private Sub PRO_DETALLELISTAPRECIOSDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles PRO_DETALLELISTAPRECIOSDataGridView.KeyDown
        If e.Control Then
            Select Case e.KeyCode
                Case Keys.B
                    If PRO_DETALLELISTAPRECIOSDataGridView.CurrentCellAddress.X = 1 Then
                        Dim fLProd As New fListaProductos
                        If fLProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                            PRO_DETALLELISTAPRECIOSDataGridView.Rows(PRO_DETALLELISTAPRECIOSDataGridView.CurrentRow.Index).Cells(1).Value = fLProd.idArticulo
                            Me.txtDescripProducto.Text = daProductos.GetNombreProducto(fLProd.idArticulo)
                        End If
                    End If
            End Select
        End If
    End Sub

    Private Sub PRO_DETALLELISTAPRECIOSDataGridView_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles PRO_DETALLELISTAPRECIOSDataGridView.RowEnter
        If e.RowIndex >= 0 AndAlso PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(1).Value IsNot Nothing AndAlso PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(1).Value IsNot DBNull.Value Then
            Me.txtDescripProducto.Text = daProductos.GetNombreProducto(PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(1).Value)
        Else
            Me.txtDescripProducto.Text = ""
        End If
    End Sub


End Class